package com.apexedu.portal.controller;

import com.apexedu.framework.util.DBUtil;
import com.apexedu.portal.constant.ActionConstant;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 同步用户
 * Created by ly on 2014/8/13.
 */
@Controller
public class RuleController {
    /**
     * 初始化用户权限
     *
     * @return
     */
    @RequestMapping(value = ActionConstant.Rule.RequestMapping_init, method = RequestMethod.GET)
    @ResponseBody
    public String initRoleUser() {
        long c = DBUtil.count(" from t_cs_user a where not exists (select null from t_sys_role b where a.rolename=b.rolename)");
        if (c > 0) {
            return "有用户对应的角色不存在";
        }
        DBUtil.executeSQL("insert into t_sys_user(userid,userloginid,userdepart,usertype) select sys_guid(),a.userloginid,'000',c.yhlx from T_CS_USER a " +
                " inner join tuser c on a.userloginid=c.userid" +
                " where not exists (select null from t_sys_user b where b.userloginid=a.userloginid)");
        DBUtil.executeSQL("insert into t_sys_user_role(id,userid,roleid)" +
                " select sys_guid(),a.userid,c.roleid" +
                " from t_sys_user a inner join t_cs_user b on a.userloginid=b.userloginid" +
                " inner join t_sys_role c on c.rolename=b.ROLENAME" +
                " where not exists (select null from t_sys_user_role d where a.userid=d.userid and d.roleid=c.roleid)");
        DBUtil.executeSQL("delete from t_cs_user");
        return "同步成功";
    }
}
